import csv

# 创建电影ID到电影名的映射字典
movie_id_to_name = {}
with open('D:\\BaiduNetdiskDownload\\movies.csv', mode='r', encoding='utf-8-sig') as file:
    reader = csv.DictReader(file)
    for row in reader:
        movie_id_to_name[row['MOVIE_ID']] = row['NAME']

# 读取comments.csv并替换MOVIE_ID
comments_updated = []
with open('D:\\BaiduNetdiskDownload\\comments1.csv', mode='r', encoding='utf-8-sig') as file:
    reader = csv.DictReader(file)
    for row in reader:
        new_row = {}
        for k, v in row.items():
            if k == 'MOVIE_ID':
                new_row['MOVIE_NAME'] = movie_id_to_name.get(v, v)
            else:
                new_row[k] = v
        comments_updated.append(new_row)

# 写入更新后的comments.csv
with open('D:\\BaiduNetdiskDownload\\comments_updated.csv', mode='w', encoding='utf-8-sig', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=comments_updated[0].keys())
    writer.writeheader()
    writer.writerows(comments_updated)

# 读取ratings.csv并替换MOVIE_ID
ratings_updated = []
with open('D:\\BaiduNetdiskDownload\\ratings.csv', mode='r', encoding='utf-8-sig') as file:
    reader = csv.DictReader(file)
    for row in reader:
        new_row = {}
        for k, v in row.items():
            if k == 'MOVIE_ID':
                new_row['MOVIE_NAME'] = movie_id_to_name.get(v, v)
            else:
                new_row[k] = v
        ratings_updated.append(new_row)

# 写入更新后的ratings.csv
with open('D:\\BaiduNetdiskDownload\\ratings_updated.csv', mode='w', encoding='utf-8-sig', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=ratings_updated[0].keys())
    writer.writeheader()
    writer.writerows(ratings_updated)
